/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-19
*/
package com.rzico.basics.controller.admin;

import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.SupplierCategory;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.SupplierCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商分类控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-19
 */
@Api(description = "供应商分类接口")
@RestController
@RequestMapping("/admin/supplierCategory")
public class SupplierCategoryAdminController extends BaseController {

    @Autowired
    private SupplierCategoryService supplierCategoryService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询客户分类
     *
     * @return
     */
    @ApiOperation("分页查询供应商分类")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分类ID", dataType = "Long", paramType = "query")})
    public CommResult<SupplierCategory> list(Long id) {
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        if (null != id){
            params.put("treePath", id);
        }

        params.put("sortField", "tree_path");
        params.put("sortType", "asc");
        List<SupplierCategory> list = supplierCategoryService.selectTree(params);
        return CommResult.success(list);
    }

    /**
     * 查询单条供应商分类
     *
     * @return
    */
    @ApiOperation("查询单条供应商分类")
    @GetMapping("/find/{id}")
    public CommResult<SupplierCategory> find(@PathVariable String id) {
        SupplierCategory result = supplierCategoryService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("supplierCategory",result);
        return CommResult.success(result);

    }

    /**
     * 保存供应商分类
     *
     * @param supplierCategory
     * @return
     */
    @ApiOperation("保存供应商分类")
    @PostMapping("/save")
    public CommResult<SupplierCategory> save(@RequestBody SupplierCategory supplierCategory) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            supplierCategory.setEnterpriseId(enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        supplierCategory.setIsShow(true);
        int affectCount = supplierCategoryService.insertUseGeneratedKeys(supplierCategory);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        SupplierCategory result = supplierCategoryService.findById(supplierCategory.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除供应商分类
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除供应商分类", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除供应商分类,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<SupplierCategory> del(@PathVariable String ids) {

        supplierCategoryService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新供应商分类
     *
     * @param supplierCategory
     * @return
     */
    @Log(desc = "更新供应商分类", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新供应商分类")
    @PostMapping("/update")
    public CommResult<SupplierCategory> update(@RequestBody SupplierCategory supplierCategory) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            supplierCategory.setEnterpriseId(enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        int affectCount = supplierCategoryService.updateByPrimaryKeySelective(supplierCategory);
        if (affectCount <= 0){
            return CommResult.error();
        }
        SupplierCategory result = supplierCategoryService.findById(supplierCategory.getId());
        return CommResult.success(result);
    }

}
